/*
 * Copyright (C) 2008 Alessandro Sivieri <alessandro.sivieri@chimera-bellerofonte.eu>.
 *
 *  This file is part of Daex.
 *
 *  Daex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.chimera_bellerofonte.tesi.ui;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import eu.chimera_bellerofonte.tesi.DaexIcon;
import eu.chimera_bellerofonte.tesi.DaexProperties;


/**
 * Classe di descrizione del dialogo in cui si settano i dettagli di
 * impostazione per la fase di ripping ed encoding.
 * 
 * @author Sivieri Alessandro
 * @author Vitucci Nicola
 * @version 14/mag/07
 */
public class SettingsDialog extends JPanel
{
	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JLabel jLabel3 = null;
	private JTextField jTextField = null;
	private JButton jButton = null;
	private JTextField jTextField1 = null;
	private JButton jButton1 = null;
	private JSpinner jSpinner = null;

	/**
	 * This is the default constructor
	 */
	public SettingsDialog(int n)
	{
		super();
		initialize(n);
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize(int n)
	{
		GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
		gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints8.gridy = 3;
		gridBagConstraints8.weightx = 1.0D;
		gridBagConstraints8.weighty = 0.25D;
		gridBagConstraints8.gridx = 1;
		GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		gridBagConstraints7.gridx = 3;
		gridBagConstraints7.fill = GridBagConstraints.BOTH;
		gridBagConstraints7.insets = new Insets(5, 10, 5, 10);
		gridBagConstraints7.weightx = 0.3D;
		gridBagConstraints7.weighty = 0.25D;
		gridBagConstraints7.gridy = 2;
		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints6.gridy = 2;
		gridBagConstraints6.weightx = 1.0D;
		gridBagConstraints6.gridwidth = 2;
		gridBagConstraints6.weighty = 0.25D;
		gridBagConstraints6.gridx = 1;
		GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
		gridBagConstraints5.gridx = 3;
		gridBagConstraints5.fill = GridBagConstraints.BOTH;
		gridBagConstraints5.weightx = 0.3D;
		gridBagConstraints5.weighty = 0.25D;
		gridBagConstraints5.insets = new Insets(5, 10, 5, 10);
		gridBagConstraints5.gridy = 1;
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints4.gridy = 1;
		gridBagConstraints4.weightx = 1.0D;
		gridBagConstraints4.gridwidth = 2;
		gridBagConstraints4.weighty = 0.25D;
		gridBagConstraints4.insets = new Insets(0, 0, 0, 0);
		gridBagConstraints4.gridx = 1;
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.gridx = 0;
		gridBagConstraints3.anchor = GridBagConstraints.WEST;
		gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints3.weightx = 0.3D;
		gridBagConstraints3.weighty = 0.25D;
		gridBagConstraints3.gridy = 3;
		jLabel3 = new JLabel();
		jLabel3.setText("Qualita' degli encoder");
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.anchor = GridBagConstraints.WEST;
		gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints2.weightx = 0.3D;
		gridBagConstraints2.weighty = 0.25D;
		gridBagConstraints2.gridy = 2;
		jLabel2 = new JLabel();
		jLabel2.setText("Directory files finali");
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 0;
		gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints1.weightx = 0.3D;
		gridBagConstraints1.weighty = 0.25D;
		gridBagConstraints1.anchor = GridBagConstraints.WEST;
		gridBagConstraints1.gridy = 1;
		jLabel1 = new JLabel();
		jLabel1.setText("Directory files temporanei");
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridwidth = 4;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0D;
		gridBagConstraints.weighty = 0.25D;
		gridBagConstraints.gridy = 0;
		jLabel = new JLabel();
		if(n==1)
		{
			jLabel.setText("Sara' codificata 1 traccia");
		}
		else
		{
			jLabel.setText("Saranno codificate "+n+" tracce");
		}
		jLabel.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 12));
		this.setSize(442, 134);
		this.setLayout(new GridBagLayout());
		this.add(jLabel, gridBagConstraints);
		this.add(jLabel1, gridBagConstraints1);
		this.add(jLabel2, gridBagConstraints2);
		this.add(jLabel3, gridBagConstraints3);
		this.add(getJTextField(), gridBagConstraints4);
		this.add(getJButton(), gridBagConstraints5);
		this.add(getJTextField1(), gridBagConstraints6);
		this.add(getJButton1(), gridBagConstraints7);
		this.add(getJSpinner(), gridBagConstraints8);
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField()
	{
		if (jTextField == null)
		{
			jTextField = new JTextField();
			jTextField.setEditable(false);
			File f=new File(DaexProperties.getProperty("directory.temp"));
			if(!f.isDirectory())
			{
				f.mkdirs();
			}
			jTextField.setText(f.getAbsolutePath());
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton()
	{
		if (jButton == null)
		{
			jButton = new JButton();
			jButton.setIcon(new ImageIcon(getClass().getResource(DaexIcon.OPENFOLDER)));
			jButton.setToolTipText("Scegli una directory differente");
			jButton.setMnemonic(KeyEvent.VK_T);
			jButton.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					String initial;
					if(getJTextField().getText().length()>0)
					{
						initial=getJTextField().getText();
					}
					else
					{
						initial=System.getProperty("user.home");
					}
					String[] result=FileChooserUtility.loadFile(SettingsDialog.this, initial, "Seleziona la directory", true, true);
					if(result!=null && result.length==1)
					{
						getJTextField().setText(result[0]);
					}
				}
			});
			if(System.getProperty("os.name").contains("Windows"))
				jButton.setEnabled(false);
		}
		return jButton;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1()
	{
		if (jTextField1 == null)
		{
			jTextField1 = new JTextField();
			jTextField1.setEditable(false);
			File f=new File(DaexProperties.getProperty("directory.out"));
			if(!f.isDirectory())
			{
				f.mkdirs();
			}
			jTextField1.setText(f.getAbsolutePath());
		}
		return jTextField1;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1()
	{
		if (jButton1 == null)
		{
			jButton1 = new JButton();
			jButton1.setIcon(new ImageIcon(getClass().getResource(DaexIcon.OPENFOLDER)));
			jButton1.setToolTipText("Scegli una directory differente");
			jButton1.setMnemonic(KeyEvent.VK_F);
			jButton1.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					String initial;
					if(getJTextField1().getText().length()>0)
					{
						initial=getJTextField1().getText();
					}
					else
					{
						initial=System.getProperty("user.home");
					}
					String[] result=FileChooserUtility.loadFile(SettingsDialog.this, initial, "Seleziona la directory", true, true);
					if(result!=null && result.length==1)
					{
						getJTextField1().setText(result[0]);
					}
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jSpinner	
	 * 	
	 * @return javax.swing.JSpinner
	 */
	private JSpinner getJSpinner()
	{
		if (jSpinner == null)
		{
			jSpinner = new JSpinner();
			jSpinner.setModel(new SpinnerNumberModel(3, 1, 10, 1));
		}
		return jSpinner;
	}
	
	/**
	 * Metodo per ottenere il valore di qualita' come intero.
	 * 
	 * @return il valore prescelto per la qualita'
	 */
	public int getQuality()
	{
		return ((Integer) getJSpinner().getValue()).intValue();
	}
	
	/**
	 * Restituisce la directory temporanea prescelta.
	 * 
	 * @return la directory temporanea
	 */
	public String getTempDir()
	{
		return getJTextField().getText();
	}
	
	/**
	 * Restituisce la directory di uscita prescelta.
	 * 
	 * @return la directory di output
	 */
	public String getOutDir()
	{
		return getJTextField1().getText();
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
